import Common from '@/utils/common'
import { SysApi } from '@/api/index'
// import { UserMock } from '@/mock/index'
const User = {
	state: {
		adminId: '',
		token: Common.getCookie('user_token'),
		adminName: '',
		usertype: '',
		userRole: '',
		uuid: '',
		subjectName: '',
		subjectId: '',
		schoolName: '',
		examSubjectId: '',
		paperId: '',
		examStatus: ''
	},
	mutations: {
		SET_ADMINID: (state, adminId) => {
			state.adminId = adminId
		},
		SET_TOKEN: (state, token) => {
			state.token = token
		},
		SET_ADMIN_NAME: (state, adminName) => {
			state.adminName = adminName
		},
		SET_USER_TYPE: (state, usertype) => {
			state.usertype = usertype
		},
		SET_ADMIN_ROLEID: (state, userrole) => {
			state.userRole = userrole
		},
		SET_USER_UUID: (state, uuid) => {
			state.uuid = uuid
		},
		SET_SCHOOLNAME: (state, schoolName) => {
			state.schoolName = schoolName
		}
	},
	actions: {
		login ({ commit }, userInfo) {
			// const username = userInfo.username.trim()
			return new Promise((resolve, reject) => {
				// UserMock.loginMock()
				SysApi.login({
					username: userInfo.username,
					password: userInfo.password
				}).then(response => {
					Common.setCookie('user_token', response.adminId, 1)
					commit('SET_ADMINID', response.adminId)
					// commit('SET_TOKEN', response.id)
					commit('SET_ADMIN_NAME', response.adminName)
					// commit('SET_ADMIN_ROLEID', response.role)
					// commit('SET_USER_TYPE', response.type)
					// commit('SET_SCHOOLNAME', response.schoolName)
					resolve()
				}).catch((error) => {
					reject(error)
				})
			})
		},
		loginByuserId ({ commit }, adminId) {
			// const username = userInfo.username.trim()
			return new Promise((resolve, reject) => {
				// UserMock.loginMock()
				SysApi.user({
					adminId: adminId
				}).then(response => {
					Common.setCookie('user_token', response.adminId, 1)
					commit('SET_ADMINID', response.adminId)
					// commit('SET_TOKEN', response.id)
					commit('SET_ADMIN_NAME', response.adminName)
					commit('SET_ADMIN_ROLEID', response.roleId)
					// commit('SET_USER_TYPE', response.type)
					commit('SET_SCHOOLNAME', response.schoolName)
					resolve()
				}).catch((error) => {
					reject(error)
				})
			})
		},
		GetInfo ({commit, state}) {
			// const username = userInfo.username.trim()
			return new Promise((resolve, reject) => {
				// UserMock.loginMock()
				SysApi.user({
					adminId: state.token
				}).then(response => {
					Common.setCookie('user_token', response.adminId, 0)
					commit('SET_ADMINID', response.adminId)
					// commit('SET_TOKEN', response.id)
					commit('SET_ADMIN_NAME', response.adminName)
					commit('SET_ADMIN_ROLEID', response.roleId)
					// commit('SET_USER_TYPE', response.type)
					commit('SET_SCHOOLNAME', response.schoolName)
					resolve()
				}).catch((error) => {
					reject(error)
				})
			})
		},
		// 登出
		logout ({ commit, state }) {
			return new Promise((resolve, reject) => {
				console.log('logout')
				commit('SET_ADMINID', '')
				commit('SET_TOKEN', '')
				commit('SET_ADMIN_NAME', '')
				commit('SET_ADMIN_ROLEID', '')
				commit('SET_USER_TYPE', '')
				Common.delCookie('user_token')
				Common.delCookie('user_type')
				Common.delCookie('user_role')
				resolve()
			})
		}
	}
}
export default User
